<?php
namespace util;

class TimeUtil
{
    public static function getCurrentDatetime()
    {
        return date("Y-m-d H:i:s" ,time());
    }

    public static function getCurrentDate()
    {
        return date("Y-m-d" ,time());
    }

    public static function getCurrentFriendlyDatetime()
    {
        return date("Y年m月d日",time());
    }

    public static function tsToYmd($ts)
    {
        return date("Y-m-d" ,$ts);
    }

    /**
     * 获取毫秒部分的代码
     * */
    function msectime() {
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        return $msectime;
    }

    /**
     * 时间戳转日期字符串
     * @param $ts
     * @return false|string
     */
    public static function tsToString($ts) {
        if($ts > 1000000000000) $ts = $ts / 1000;
        return date("Y-m-d H:i:s",$ts);
    }


    public static function getDateRange($start, $end) {
        $start = \strtotime($start);
        $end = \strtotime($end);
        $result = [];
        $count = 0;
        while($start <= $end && $count < 1000) {
            array_push($result, date("Y-m-d",$start));
            $start = strtotime('+1 day', $start);
        }
        return $result;
    }

    /**
     * 获取当前年份
     * @return false|string
     */
    public static function getCurrentYear(){
        return date('Y');
    }

    /**
     * 获取当前月份
     * @return false|string
     */
    public static function getCurrentMonth() {
        return date('m');
    }

    /**
     * 获取当前日期
     * @return false|string
     */
    public static function getCurrentDay() {
        return date('d');
    }

    /**
     * 获取某月最后一天
     * @param $year
     * @param $month
     * @return int
     */
    public static function getEndDay($year, $month) {
        $monthStartTs = self::getMonthStarTs($year, $month);
        return date("t",$monthStartTs);
    }

    /**
     * 获取今天第一秒
     * @return int
     */
    public static function getTodayStarTs(){
        return mktime(0,0,0,date('m'),date('d'),date('Y'));
    }

    /**
     * 获取今天最后一秒
     * @return int
     */
    public static function getTodayEndTs(){
        return mktime(0,0,0,date('m'),date('d')+1,date('Y')) - 1;
    }

    /**
     * 获取本周第一秒
     * @return int
     */
    public static function getThisWeekStarTs(){
        return mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
    }

    /**
     * 获取本周最后一秒
     * @return int
     */
    public static function getThisWeekEndTs(){
        return mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
    }


    /**
     * 获取某月的第一秒
     * @param null $year
     * @param null $month
     * @return false|int
     */
    public static function getMonthStarTs($year = null, $month = null) {
        if($month == null) {
            $month = self::getCurrentMonth();
        }
        if($year == null){
            $year = self::getCurrentYear();
        }
        return mktime(0,0,0, $month,1 ,$year);
    }

    /**
     * 获取某月的第最后一秒
     * @param null $year
     * @param null $month
     * @return false|int
     */
    public static function getMonthEndTs($year = null, $month = null) {
        if($month == null) {
            $month = self::getCurrentMonth();
        }
        if($year == null){
            $year = self::getCurrentYear();
        }
        $day = self::getEndDay($year, $month);
        return mktime(23,59,59, $month, $day ,$year);
    }

    /**
     * 获取某年的第一秒
     * @param null $year
     * @return false|int
     */
    public static function getYearStarTs($year = null){
        if($year == null) {
            $year = self::getCurrentYear();
        }
        return mktime(0,0,0,1,1,$year);
    }

    /**
     * 获取某年的最后一秒
     * @param null $year
     * @return false|int
     */
    public static function getYearEndTs($year = null) {
        if($year == null) {
            $year = self::getCurrentYear();
        }
        $endDay = self::getEndDay($year, 12);
        return mktime(23,59,59,12,$endDay,$year);
    }

    public static function getDayStartTs($year = null, $month = null, $day = null)
    {
        if($month == null) {
            $month = self::getCurrentMonth();
        }
        if($year == null){
            $year = self::getCurrentYear();
        }
        if($day == null){
            $day = self::getCurrentDay();
        }
        return mktime(0,0,0,$month, $day, $year);
    }

    public static function getDayEndTs($year = null, $month = null, $day = null)
    {
        $start = self::getDayStartTs($year, $month, $day);
        $end = $start + 24 * 60 * 60 - 1;
        return $end;
    }


    /**
     * 判断是否比某个时间点更早
     */
    public static function isEarilyerThen($dateTime, $standerTime) {
        return strtotime($dateTime) < strtotime($standerTime);
    }

    /**
     * 判断是否在时间内
     */
    public static function isBetween($startTime, $endTime) {
        $start = strtotime($startTime);
        $end = strtotime($endTime);
        $now = time();
        return $now >= $start && $now <= $end;
    }

    static function diffYear($a, $b = '') {
        if(empty($b)) $b = date('Y-m-d');
        $res = self::diffDate($a, $b);
        return $res['year'];
    }

    /*
    *function：计算两个日期相隔多少年，多少月，多少天
    *param string $date1[格式如：2011-11-5]
    *param string $date2[格式如：2012-12-01]
    *return array array('年','月','日');
    */
    static function diffDate($date1,$date2 = ''){
        if(empty($date2)) $date2 = date('Y-m-d');
        if(strtotime($date1)>strtotime($date2)){
            $tmp=$date2;
            $date2=$date1;
            $date1=$tmp;
        }
        list($Y1,$m1,$d1)=explode('-',$date1);
        list($Y2,$m2,$d2)=explode('-',$date2);
        $Y=$Y2-$Y1;
        $m=$m2-$m1;
        $d=$d2-$d1;
        if($d<0){
            $d+=(int)date('t',strtotime("-1 month $date2"));
            $m--;
        }
        if($m<0){
            $m+=12;
            $Y--;
        }
        return array('year'=>$Y,'month'=>$m,'day'=>$d);
    }
}